package com.hns.can.base.web;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hns.can.base.entity.SysCorpInfoEntity;
import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.base.service.SysCorpInfoService;
import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.web.BaseController;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.util.ReflectUtil;

/**
 * 公司信息设置控制层
 * @author:JeffXu
 * @email:835264047@qq.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/sysCorpInfo")
public class SysCorpInfoController extends BaseController {
	
	/**
	 * 公司信息设置服务层对象
	 */
	@Autowired
	private SysCorpInfoService sysCorpInfoService;
	
	@Autowired
    private FillCodeDscValue codeDscValue;
	
	/**
	 * 公司信息设置 - 获取当前用户所属的公司信息
	 * @return 公司实体
	 * @author:JeffXu
	 * @email:835264047@qq.com
	 */
	@RequestMapping("/searchCurrSysCorpInfo")
	@ResponseBody
    public Object searchCurrSysCorpInfo(){
		SysUserEntity user = getCurrentUser().getUser();
        String corpId = user.getCorpId();
        BaseEntity corpInfo = sysCorpInfoService.searchSysCorpInfoByCorpId(corpId);
        return this.getReturnResult(corpInfo);
    }
	
	/**
	 * 取得企业树
	 * @return 企业list
	 * @author:cwm
	 * @email:112362667@qq.com
	 */
    @RequestMapping("/getCorpTree")
    @ResponseBody
    public Object getCorpTree(){
    	SysUserEntity user = getCurrentUser().getUser();
        List list = sysCorpInfoService.getCorpTree(user.getCorpId());
        
        Map<String,String> map = new HashMap<String,String>();
        for(Object entity : list){
            map.put("cityName", ReflectUtil.getFieldValue(entity, "provinceId").toString());
        }
        codeDscValue.fillCodeDscValue(list, map);
        return this.getReturnResult(list);
    }
    
    /**
     * 得到企业信息的全部数据，用来展示树
     * @return 公司信息list
     * @author:JeffXu
     * @email:835264047@qq.com
     */
    @RequestMapping("/getSysCorpInfoList")
    @ResponseBody
    public Object getSysCorpInfoList(){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        List<SysCorpInfoEntity> list = sysCorpInfoService.getSysCorpInfoList(paramMap);
        return this.getReturnResult(list);
    }
    
    /**
     * 判断权限,如果有对应权限才能得到企业信息的全部数据，用来展示树
     * @return 公司信息list
     * @author:Chenl
     * @email:370238764@qq.com
     */
    @RequestMapping("/getJurisdictionCorpInfoList")
    @ResponseBody
    public Object getJurisdictionCorpInfoList(){
    	SysUserEntity user = this.getCurrentUser().getUser();
        String corpId = user.getCorpId();

//    	if("1".equals(user.getIsAdminRole())){
    		Map<String, Object> paramMap = new HashMap<String, Object>();
    		List<SysCorpInfoEntity>	list = sysCorpInfoService.getSysCorpInfoList(paramMap);
            return this.getReturnResult(list);
//    	}else{
//            BaseEntity corpInfo = sysCorpInfoService.searchSysCorpInfoByCorpId(corpId);
//            return this.getReturnResult(corpInfo);
//    	}
    }
    
    /**
     * 公司信息设置 - 更新当前用户所属的公司信息
     * @param corpInfo 公司信息
     * @return 返回消息实体
     * @throws Exception 值对象拷贝异常
     * @author:JeffXu
     * @email:835264047@qq.com
     */
    @RequestMapping("/updateSysCorpInfo")
    @ResponseBody
    public Object updateSysCorpInfo(SysCorpInfoEntity corpInfo) throws Exception {
        sysCorpInfoService.updateSysCorpInfo(corpInfo, getCurrentUser().getUser());
        return getReturnResult();
    }
    
    /**
     * 公司信息设置 - 更新当前用户所属的公司信息
     * @param corpInfo 公司信息
     * @return 返回消息实体
     * @throws Exception 值对象拷贝异常
     * @author:JeffXu
     * @email:835264047@qq.com
     */
    @RequestMapping("/updateSysCorpInfo2")
    @ResponseBody
    public Object updateSysCorpInfo2(SysCorpInfoEntity corpInfo) throws Exception {
//    	int countCode= sysCorpInfoService.getCorpByCode(corpInfo);
//        if(countCode>0){
//            throw new BusinessException(MsgType.WARN.getFlag(), "您输入的编码已存在");
//        }
        sysCorpInfoService.updateSysCorpInfo2(corpInfo, getCurrentUser().getUser());
        return this.getReturnResult(corpInfo.getCorpId());
    }
    
    /**
     * 新增一个企业信息
     * @param corpInfo 公司信息
     * @return 返回消息实体
     * @throws Exception 保存异常
     * @author:JeffXu
     * @email:835264047@qq.com
     */
    @RequestMapping("/saveSysCorpInfo")
    @ResponseBody
    public Object saveSysCorpInfo(SysCorpInfoEntity corpInfo) throws Exception {
//    	int countCode= sysCorpInfoService.getCorpByCode(corpInfo);
//        if(countCode>0){
//            throw new BusinessException(MsgType.WARN.getFlag(), "您输入的编码已存在");
//        }
    	String corpId = sysCorpInfoService.saveSysCorpInfo(corpInfo, getCurrentUser().getUser());
    	return getReturnResult(corpId);
    }
    
    /**
     * 删除一个企业信息
     * @param corpId 公司信息
     * @return 返回消息实体
     * @throws Exception 删除异常
     * @author:JeffXu
     * @email:835264047@qq.com
     */
    @RequestMapping("/delSysCorpInfo")
    @ResponseBody
    public Object delSysCorpInfo(@RequestParam("corpId") String corpId) throws Exception {
    	sysCorpInfoService.delSysCorpInfo(corpId, getCurrentUser().getUser());
    	return getReturnResult();
    }

    /**
     * 图片上传
     *
     * @param myfiles         图片文件
     * @param uploadPath上传的路径
     * @return
     * @throws Exception
     * @return: Object
     */
    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
    @ResponseBody
    public Object uploadImage(@RequestParam MultipartFile[] logo, String uploadPath, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	SysUserEntity user = getCurrentUser().getUser();
    	String corpId = user.getCorpId();
    	String realPath = sysCorpInfoService.fileUpload(logo, corpId, uploadPath, request, response);
//    	resizeImage( realPath, corpId, 170, 52);
        return null;
    }
    
    /**
     * 保存公司logo和车辆树显示方式到数据库中
     */
    @RequestMapping("/saveCorpLogo")
    @ResponseBody
    public Object saveCorpLogo(@RequestParam("url") String url,String carNameMode,String corpId){
    	System.out.println("url:"+url);
    	String msg = sysCorpInfoService.saveCorpLogo(url,carNameMode,getCurrentUser().getUser(),corpId);
    	return getReturnResult(msg);
    }
    
    /**
     * 加载企业logo
     */
    @RequestMapping("/loadCorpLogo")
    @ResponseBody
    public Object loadCorpLogo(String corpId){
    	SysCorpInfoEntity entity = sysCorpInfoService.loadCorpLogo(getCurrentUser().getUser(),corpId);
    	return getReturnResult(entity);
    }
    
    public static void resizeImage(String imagePath, String corpId,int width, int height) throws IOException{
		File srcFile = new File(imagePath); 
		Image srcImg = ImageIO.read(srcFile);  
        BufferedImage buffImg = null;  
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
        buffImg.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
        srcFile.delete();
        ImageIO.write(buffImg, "JPEG", new File(imagePath));  
	}
	
}
